package org.fqh.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {
	 
	private static final Logger log = LoggerFactory.getLogger(LogAspect.class);  
	
	@Before("execution(* org.fqh.service.impl.*.*(..))")
    public void before(JoinPoint jp){
        log.info("----------前置通知----------");
        log.info(jp.getSignature().getName());
    }
//	@After("execution(* org.fqh.system.controller.*.*(..))")
//    public void after(JoinPoint jp){
//        log.info("----------最终通知----------");
//    }
	
	@Around("execution(* org.fqh.service.impl.*.*(..))")
    public Object timeAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // 定义返回对象、得到方法需要的参数
        Object obj = null;
        Object[] args = joinPoint.getArgs();
        long startTime = System.currentTimeMillis();
        
        obj = joinPoint.proceed(args);
 
        //获取类名  
        String className = joinPoint.getTarget().getClass().getSimpleName();  
        // 获取执行的方法名
        long endTime = System.currentTimeMillis();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        String methodName = signature.getDeclaringTypeName() + "." + signature.getName();

        // 打印耗时的信息
        //  this.printExecTime(methodName, startTime, endTime);
        log.info(methodName+":运行时间:"+(endTime-startTime)+"ms");
        return obj;
    }

	
}
